package com.cumin.shiyin.core.config;

import lombok.SneakyThrows;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @author YangJian.Kan
 * @version 1.0
 * @date 2020/4/14 15:10
 * 加载模块默认配置文件
 */
public class CoreEnvironmentProcessor implements EnvironmentPostProcessor {

    private static final String CONFIG_FILE_NAME = "application-sy-core.yml";

    @SneakyThrows
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Resource resource = new ClassPathResource(CONFIG_FILE_NAME);
        YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
        PropertySource<?> propertySource = yamlPropertySourceLoader.load(CONFIG_FILE_NAME, resource).get(0);
        environment.getPropertySources().addLast(propertySource);
    }
}
